package com.itheima.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.ClazzService;
import com.itheima.controller.DeptController;
import com.itheima.entity.*;
import com.itheima.mapper.ClazzMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 班级业务处理层
 * @author cc
 * @date 2020-09-07-14:01
 */
@Service
public class ClazzServiceImpl implements ClazzService {
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public List<Clazz> list() {
        // 1.调用mapper层的list()方法，并返回数据
        return clazzMapper.list();
    }

    /**
     * 分页查询
     * @param param
     * @return
     */
    @Override
    public PageBean page(ClazzQueryParam param) {
        // 1.设置分页参数
        PageHelper.startPage(param.getPage(), param.getPageSize());

        // 2.调用mapper的列表查询方法
        List<Clazz> clazzList = clazzMapper.page(param);
        Page p = (Page) clazzList;

        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 删除班级
     * @param id
     */
    @Override
    public void delete(Integer id) {
        clazzMapper.deleteByID(id);
    }

    /**
     * 添加班级
     * @param clazz
     */
    @Override
    public void add(Clazz clazz) {
        // 获取当前时间 格式：yyyy-MM-dd HH:mm:ss
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        log.info("add:{}", clazz);
        clazzMapper.add(clazz);
    }

    /**
     * 根据id查询班级
     * @param id
     * @return
     */
    @Override
    public Clazz getById(Integer id) {
        return clazzMapper.getById(id);
    }

    @Transactional //开启事务
    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        log.info("update:{}", clazz);
        clazzMapper.update(clazz);
    }
}
